package com.moorror.codegenvel.db.mysql.utils;

import org.apache.commons.lang3.tuple.Pair;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @Author jiyanghuang
 * @Date 2025/10/25 10:23
 */public class JdbcUtils {

    public static Pair<Boolean, String> connectionTest(String url, String user, String password){

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection(url, user, password);
            if(null != conn){
                conn.close();
                return Pair.of(true,"success");
            }
            return Pair.of(false,"connect error");
        } catch (Exception e) {
            e.printStackTrace();
            return Pair.of(false,e.getMessage());
        }
    }

    public static Pair<Boolean, String> connectionTestLocal() {
        String url = "jdbc:mysql://localhost:3306/trade_single_sys?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true"; // 注意这里不指定具体数据库名，因为我们将在运行时获取数据库列表。
        String user = "root12";
        String password = "root123456";
        return connectionTest(url, user, password);
    }
}
